package com.new_wear.project.system.volunteer.controller;

import java.util.List;

import com.new_wear.common.exception.BusinessException;
import com.new_wear.project.system.access.domain.CstVolunteerGroupAccess;
import com.new_wear.project.system.access.service.ICstVolunteerGroupAccessService;
import com.new_wear.project.system.group.domain.CstVolunteerGroup;
import com.new_wear.project.system.group.service.ICstVolunteerGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.volunteer.domain.CstVolunteer;
import com.new_wear.project.system.volunteer.service.ICstVolunteerService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统 - 志愿者Controller
 * 
 * @author new_wear
 * @date 2019-12-31
 */
@Controller
@RequestMapping("/system/volunteer")
public class CstVolunteerController extends BaseController
{
    private String prefix = "system/volunteer";

    @Autowired
    private ICstVolunteerService cstVolunteerService;
    @Autowired
    private ICstVolunteerGroupService cstVolunteerGroupService;
    @Autowired
    private ICstVolunteerGroupAccessService cstVolunteerGroupAccessService;
    @RequiresPermissions("system:volunteer:view")
    @GetMapping()
    public String volunteer()
    {
        return prefix + "/volunteer";
    }

    /**
     * 查询系统 - 志愿者列表
     */
    @RequiresPermissions("system:volunteer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstVolunteer cstVolunteer)
    {
        startPage();
        List<CstVolunteer> list = cstVolunteerService.selectCstVolunteerList(cstVolunteer);
        return getDataTable(list);
    }

    /**
     * 导出系统 - 志愿者列表
     */
    @RequiresPermissions("system:volunteer:export")
    @Log(title = "系统 - 志愿者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstVolunteer cstVolunteer)
    {
        List<CstVolunteer> list = cstVolunteerService.selectCstVolunteerList(cstVolunteer);
        ExcelUtil<CstVolunteer> util = new ExcelUtil<CstVolunteer>(CstVolunteer.class);
        return util.exportExcel(list, "volunteer");
    }

    /**
     * 新增系统 - 志愿者
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 志愿者
     */
    @RequiresPermissions("system:volunteer:add")
    @Log(title = "系统 - 志愿者", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstVolunteer cstVolunteer)
    {
        return toAjax(cstVolunteerService.insertCstVolunteer(cstVolunteer));
    }

    /**
     * 修改系统 - 志愿者
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstVolunteer cstVolunteer = cstVolunteerService.selectCstVolunteerById(id);
        mmap.put("cstVolunteer", cstVolunteer);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 志愿者
     */
    @RequiresPermissions("system:volunteer:edit")
    @Log(title = "系统 - 志愿者", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstVolunteer cstVolunteer)
    {
        return toAjax(cstVolunteerService.updateCstVolunteer(cstVolunteer));
    }

    /**
     * 删除系统 - 志愿者
     */
    @RequiresPermissions("system:volunteer:remove")
    @Log(title = "系统 - 志愿者", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstVolunteerService.deleteCstVolunteerByIds(ids));
    }


    /*跳转查看当前团体页面*/
    @GetMapping("/viewGroup/{id}")
    public String selcectVolunteer(@PathVariable("id") String id,ModelMap map)
    {
        map.put("id",id);
        return prefix + "/viewGroup";
    }
    /*根据团体id 查询当前团体*/
    @RequestMapping("/selctctTeer/{id}")
    @ResponseBody
    public TableDataInfo selectTeer(@PathVariable("id") String id){

        List<CstVolunteerGroupAccess> cstVolunteerGroupAccesses = cstVolunteerGroupAccessService.selectGroup(id);
        return getDataTable(cstVolunteerGroupAccesses);
    }

}
